package com.quad.innovators.salesease.service.impl.warehouse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quad.innovators.salesease.common.utils.SystemCodeUtils;
import com.quad.innovators.salesease.converter.PurchaseOrderDetailConverter;
import com.quad.innovators.salesease.enums.PaymentStatusEnum;
import com.quad.innovators.salesease.enums.ResultCode;
import com.quad.innovators.salesease.enums.SystemCodeEnum;
import com.quad.innovators.salesease.exception.BusinessException;
import com.quad.innovators.salesease.mapper.warehouse.PurchaseOrderDetailMapper;
import com.quad.innovators.salesease.model.entity.warehouse.PurchaseOrder;
import com.quad.innovators.salesease.model.entity.warehouse.PurchaseOrderDetail;
import com.quad.innovators.salesease.model.form.warehouse.PurchaseOrderDetailForm;
import com.quad.innovators.salesease.service.warehouse.PurchaseOrderDetailService;

import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import lombok.RequiredArgsConstructor;

/**
 * @author : shanshan.li
 * @createTime : 2024/10/21 21:46
 * @Version : 1.0 
 */
@Service
@RequiredArgsConstructor
public class PurchaseOrderDetailServiceImpl extends ServiceImpl<PurchaseOrderDetailMapper, PurchaseOrderDetail> implements PurchaseOrderDetailService {

    private final PurchaseOrderDetailConverter purchaseOrderDetailConverter;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveList(String purchaseOrderCode, List<PurchaseOrderDetailForm> purchaseOrderDetailFormList) {
        return this.saveBatch(purchaseOrderDetailFormList.stream().map(model -> {
            PurchaseOrderDetail entity = purchaseOrderDetailConverter.toEntity(model);
            entity.setCode(SystemCodeUtils.getCode(SystemCodeEnum.PURCHASE_ORDER_DETAIL));
            entity.setPurchaseOrderCode(purchaseOrderCode);
            return entity;
        }).toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateList(String purchaseOrderCode, List<PurchaseOrderDetailForm> purchaseOrderDetailFormList) {
        int delete = this.baseMapper.delete(new QueryWrapper<PurchaseOrderDetail>().lambda().eq(PurchaseOrderDetail::getPurchaseOrderCode, purchaseOrderCode));
        return delete > 0 && saveList(purchaseOrderCode, purchaseOrderDetailFormList);
    }

    @Override
    public List<PurchaseOrderDetailForm> getListByPurchaseOrderCode(String purchaseOrderCode) {
        return this.baseMapper.selectList(new LambdaQueryWrapper<PurchaseOrderDetail>().eq(PurchaseOrderDetail::getPurchaseOrderCode, purchaseOrderCode))
                .stream().map(purchaseOrderDetailConverter::toForm).toList();
    }

    @Override
    public boolean updateStatus(String code) {
        PurchaseOrderDetail purchaseOrderDetail = this.baseMapper.selectOne(new QueryWrapper<PurchaseOrderDetail>().lambda().eq(PurchaseOrderDetail::getCode, code));
        if (ObjectUtils.isEmpty(purchaseOrderDetail)) {
            throw BusinessException.resultCode(ResultCode.NOT_FOUND);
        }
        purchaseOrderDetail.setPaymentStatus(PaymentStatusEnum.PAY.getCode());
        return this.updateById(purchaseOrderDetail);
    }
}
